ComponentOne PdfViewer for UWP
In This Topic
    Using the Navigation Message Box
    In This Topic

    C1PdfViewer allows you to add a message box to control navigation away from the PDF document. If a user clicks on a link in the PDF file, the application will give them the option to navigate away from the document or to cancel the navigation request.

    Note that in this step you will add a PDF file that is included with the ComponentOne Studio for Windows Phone samples, which are by default installed in the Documents folder in the ComponentOne Samples\UWP directory. If you choose, you can instead use another PDF file and adapt the steps.

    Complete these steps to create the navigation request:

    1. In the Solution Explorer, right-click on the project name and select Add | Existing Item from the menu. The Add Existing Item dialog box will appear.
    2. Select the sample PDF file, C1XapOptimizer.pdf, and click Add.
    3. Right-click the PDF file you just added in the Solution Explorer and select Properties from the menu. Set the BuildAction property to Resource and confirm that the Copy to Output Directory item is set to Do Not Copy.
    4. Open the XAML view of the Mainpage.xaml file and add the following markup to create the C1PdfViewer component:

    <c1pdfviewer:C1PdfViewer x:Name="pdfViewer" ViewMode="FitWidth" />

    1. Open the Code View by right-clicking on the document and add the following imports statement:

    To write the code in Visual Basic:

      Imports C1.Xaml.PdfViewer


    To write the code in C#:

      using C1.Xaml.PdfViewer;

    1. Add the following code after the InitializeComponent() method:


    To write the code in Visual Basic:

     

    Loaded = (Loaded + AddressOf Me.PhoneApp5_Loaded)

     pdfViewer.RequestNavigate = (pdfViewer.RequestNavigate + s)

     ,e

     Unknown=Greater{Dim result As var = MessageBox.Show(("The document is requesting to navigate to " + e.Uri), "Navigate", MessageBoxButton.OKCancel)

     If (result = MessageBoxResult.OK) Then

         ' navigate to urls loading web browser control

         Dim browser As WebBrowser = New WebBrowser

         browser.Navigate(e.Uri)

         LayoutRoot.Children.Clear

         LayoutRoot.Children.Add(browser)

     End If

     UnknownUnknownUnknownUnknown

        

         Private Sub PhoneApp5_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)

             Dim resource As var = Application.GetResourceStream(New Uri("PhoneApp5;component/C1XapOptimizer.pdf", UriKind.Relative))

             pdfViewer.LoadDocument(resource.Stream)

             'pdfViewer.PageRendered += (s2, e2) =>

             'if (e2.TotalRenderedPages >= 2)

             pdfViewer.Visibility = System.Windows.Visibility.Visible

         End Sub


    To write the code in C#:

      
    C#
    Copy Code

    Loaded += new RoutedEventHandler(PhoneApp5_Loaded);

     

                    pdfViewer.RequestNavigate += (s, e) =>

                    {

                        var result = MessageBox.Show("The document is requesting to navigate to " + e.Uri, "Navigate", MessageBoxButton.OKCancel);

                        if (result == MessageBoxResult.OK)

                        {

                            // navigate to urls loading web browser control

                            WebBrowser browser = new WebBrowser();

                            browser.Navigate(e.Uri);

     

                            LayoutRoot.Children.Clear();

                            LayoutRoot.Children.Add(browser);

                        }

                    };

                }

                     void PhoneApp5_Loaded(object sender, RoutedEventArgs e)

               {

        var resource = Application.GetResourceStream(new Uri("PhoneApp5;component/C1XapOptimizer.pdf", UriKind.Relative));

                   pdfViewer.LoadDocument(resource.Stream);

                   //pdfViewer.PageRendered += (s2, e2) =>

                   {

                       //if (e2.TotalRenderedPages >= 2)

                       {

                          pdfViewer.Visibility = System.Windows.Visibility.Visible;

     

            }

        }

    }

        }

    }

    1. Press F5 to run your application. Locate a link in the PDF document and click on it. The Navigation Message Box should appear as in the following image: